/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.tianya.irock.data.dalfactory;


import java.lang.reflect.Constructor;
import org.apache.log4j.Logger;

import cn.tianya.irock.core.Configuration;
import cn.tianya.irock.data.idal.IAttachmentDataProvider;
import cn.tianya.irock.data.idal.IForumDataProvider;
import cn.tianya.irock.data.idal.ILogDataProvider;
import cn.tianya.irock.data.idal.IPermissionDataProvider;
import cn.tianya.irock.data.idal.IMethodDataProvider;
import cn.tianya.irock.data.idal.IPostDataProvider;
import cn.tianya.irock.data.idal.IRoleDataProvider;
import cn.tianya.irock.data.idal.ISearchDataProvider;
import cn.tianya.irock.data.idal.ISectionDataProvider;
import cn.tianya.irock.data.idal.ISiteSettingsDataProvider;
import cn.tianya.irock.data.idal.IThreadDataProvider;
import cn.tianya.irock.data.idal.IUserDataProvider;


/**
 *
 * @author Texel
 */
public final class DataAccess {
	private static Logger logger = Logger.getLogger(DataAccess.class);
	
    private DataAccess(){}

    private static Object createProvider(String path, String name) {
        Object instance = null;
        
        try {
            Class<?>       provider    = Class.forName(path + "." + name);
            Constructor<?> constructor = provider.getConstructor();

            instance = constructor.newInstance();
        } catch (Exception ex) {
        	logger.error(ex); 
        }

        return instance;
    }
    
    public static IForumDataProvider createForumProvider() {
        return (IForumDataProvider)createProvider(Configuration.config().getSectionDal(), "ForumDataProvider");
    }
    
    public static IUserDataProvider createUserDataProvider() {
        return (IUserDataProvider)createProvider(Configuration.config().getUserDal(), "UserDataProvider");
    }
    
    public static IRoleDataProvider createRoleDataProvider() {
        return (IRoleDataProvider)createProvider(Configuration.config().getRoleDal(), "RoleDataProvider");
    }
    
    public static ISectionDataProvider createSectionDataProvider() {
        return (ISectionDataProvider)createProvider(Configuration.config().getSectionDal(), "SectionDataProvider");
    }
        
    public static ISiteSettingsDataProvider createSiteDataProvider() {
        return (ISiteSettingsDataProvider)createProvider(Configuration.config().getSiteDal(), "SiteSettingsDataProvider");
    }
    
    public static IPermissionDataProvider createPermissionDataProvider() {
    	return (IPermissionDataProvider)createProvider(Configuration.config().getPermissionDal(), "ForumPermissionDataProvider");
    }
    
    public static IThreadDataProvider createThreadDataProvider() {
        return (IThreadDataProvider)createProvider(Configuration.config().getThreadDal(), "ThreadDataProvider");
    }
    
    public static IPostDataProvider createPostDataProvider() {
        return (IPostDataProvider)createProvider(Configuration.config().getPostDal(), "PostDataProvider");
    }
        
    public static ILogDataProvider createLogDataProvider() {
        return (ILogDataProvider)createProvider(Configuration.config().getUrlPatternDal(), "LogDataProvider");
    }
    
    public static ISearchDataProvider createSearchDataProvider() {
    	return (ISearchDataProvider)createProvider(Configuration.config().getSearchPatternDal(), "SearchDataProvider");
    }
    
    public static IAttachmentDataProvider createAttachmentDataProvider() {
    	return (IAttachmentDataProvider)createProvider(Configuration.config().getAttachmentPatternDal(), "AttachmentDataProvider");
    }
    
    public static IMethodDataProvider createMethodProvider() {
        return (IMethodDataProvider)createProvider(Configuration.config().getAttachmentPatternDal(), "MethodDataProvider");
    }
}
